﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Input;

namespace WindowsPhone
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private CameraBanking.Camera _camera;
        private ApplicationBarMenuItem menuItem1;

        private void btnCameraQr_Tap(object sender, GestureEventArgs e)
        {
            NavigatedEventHandler navigated = null;
            navigated = new NavigatedEventHandler((a, b) =>
                {
                    (b.Content as CameraBanking.Camera).BarcodeType = CameraBanking.BarcodeType.Qrcode;
                    (b.Content as CameraBanking.Camera).BarcodeResultFoundChangedEventHandler += MainPage_BarcodeResultFoundChangedEventHandler;
                    NavigationService.Navigated -= navigated;
                });
            NavigationService.Navigated += navigated;
            
            NavigationService.
                Navigate(new Uri("/CameraBanking;component/Camera.xaml", UriKind.Relative));
        }



        private void btnCameraQrComBarra_Tap(object sender, GestureEventArgs e)
        {
            ApplicationBar _BarQrCode = new ApplicationBar();
            ApplicationBarIconButton _BtnQrCode = new ApplicationBarIconButton();
            _BtnQrCode = new ApplicationBarIconButton(new Uri("/Assets/appbar.barcode.png", UriKind.Relative));
            _BtnQrCode.Text = "Evento no chamador";
            _BtnQrCode.Click += BarBtn_Click;
            _BarQrCode.Buttons.Clear();
            _BarQrCode.Buttons.Add(_BtnQrCode);
            _BarQrCode.IsVisible = true;


            menuItem1 = new ApplicationBarMenuItem();
            menuItem1.Text = "Ocultar miniatura";
            _BarQrCode.MenuItems.Add(menuItem1);
            menuItem1.Click += menuItem1_Click;


            NavigatedEventHandler navigated = null;
            navigated = new NavigatedEventHandler((a, b) =>
                {
                    _camera = (b.Content as CameraBanking.Camera);

                    menuItem1.Text = textoDoMenu();

                    _camera.BarcodeType = CameraBanking.BarcodeType.Qrcode;
                    _camera.ApplicationBar = _BarQrCode;
                    _camera.OverrideAppBar = true;
                    //_camera.ApplicationBar.IsVisible = true;
                    _camera.BarcodeResultFoundChangedEventHandler += MainPage_BarcodeResultFoundChangedEventHandler;
                    NavigationService.Navigated -= navigated;
                });
            NavigationService.Navigated += navigated;
            
            NavigationService.
                Navigate(new Uri("/CameraBanking;component/Camera.xaml", UriKind.Relative));
        }

        void menuItem1_Click(object sender, EventArgs e)
        {
            if (_camera.ExibirHelper)
            {
                _camera.ExibirHelper = false;
                menuItem1.Text = textoDoMenu();
            }
            else
            {
                _camera.ExibirHelper = true;
                menuItem1.Text = textoDoMenu();
            }
        }

        private string textoDoMenu()
        {
            if (_camera.ExibirHelper)
            {
                return "Ocultar miniatura";
            }
            else
            {
                return "Exibir miniatura";
            }
        }

        private void btnCameraBar_Tap(object sender, GestureEventArgs e)
        {
            NavigatedEventHandler navigated = null;
            navigated = new NavigatedEventHandler((a, b) =>
            {
                (b.Content as CameraBanking.Camera).BarcodeType = CameraBanking.BarcodeType.Barcode;
                //(b.Content as CameraBanking.Camera).ApplicationBar.IsVisible = true;
                (b.Content as CameraBanking.Camera).BarcodeResultFoundChangedEventHandler += MainPage_BarcodeResultFoundChangedEventHandler;
                NavigationService.Navigated -= navigated;
            });
            NavigationService.Navigated += navigated;

            NavigationService.
                Navigate(new Uri("/CameraBanking;component/Camera.xaml", UriKind.Relative));
        }

        void MainPage_BarcodeResultFoundChangedEventHandler(object sender, CameraBanking.FoundBarcodeEventArgs e)
        {
            TextResult.Text = e.TextResult;
            FormatResult.Text = e.FormatResult;
            ImageResult.Source = e.ResultWriteableBitmap;

            //NavigationService.GoBack();
        }

        private void BarBtn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Evento localizado na página que chamou a camera.");
        }
    }
}